//
//  ZGLMatchDetailController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/10/23.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLMatchDetailController.h"
#import "ZGLDetaiHeaderView.h"
#import "ZGLIntelligencTeamCell.h"

#import "ZGLIntelligenceModel.h"
//#import "ZGLIntelligenceDataModel.h"

#import <WebKit/WebKit.h>

@interface ZGLMatchDetailController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    CGFloat srcW ;
    CGFloat srcH ;
}

@property (nonatomic, strong) ZGLDetaiHeaderView *headerView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *scrollBgView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) NSMutableArray<ZGLIntelligenceModel *> *dataArray;
@property (nonatomic, copy) NSString *winID;
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSDictionary *home;
@property (nonatomic, strong) NSDictionary *away;
@property (nonatomic, strong) ZGLIntelligencTeamCell *teamCell;

@end

@implementation ZGLMatchDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"赛事数据";
    self.view.backgroundColor = ViewBackgroundColor;
    
    srcW = UI_SCREEN_WIDTH;
    srcH = UI_SCREEN_HEIGHT - 135;
    
    [self setupUI];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self getMessageData];
    });
    
    
}

- (void)scrollToPageWith:(NSInteger )index
{
    CGPoint point = CGPointMake(index*srcW, 0);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [self.scrollView setContentOffset:point];
        
    }];
    
}

- (void)setupUI
{
    _headerView = [[ZGLDetaiHeaderView alloc]init];
    WS(weakself)
    _headerView.buttonTag = ^(NSInteger tag) {
      
        weakSelf.index = tag;
        [weakSelf scrollToPageWith:weakSelf.index];
    };
    _headerView.model = self.model;
    [self.view addSubview:self.headerView];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.mas_equalTo(130);
    }];
    
    [self.view addSubview:self.scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom).offset(5);
        make.bottom.equalTo(self.view);
        
    }];
    
    
    dispatch_async(dispatch_get_main_queue(), ^{

        for (int i = 0; i<2; i++) {

            if (i == 0) {
                
                [_scrollView addSubview:self.tableView];
                _tableView.frame = CGRectMake(i*srcW, 0, srcW, srcH-STATUEHEIGHT);

            }else{
                
                [_scrollView addSubview:self.webView];
                _webView.frame = CGRectMake(i*srcW, 0, srcW, srcH-STATUEHEIGHT);
            }
        }
        _scrollView.contentSize = CGSizeMake(2*srcW, srcH);

    });
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ZGLIntelligencTeamCell";
    ZGLIntelligencTeamCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [ZGLIntelligencTeamCell intelligenceTeamCell];
    }
    if (indexPath.row == 0) {
        
        cell.teamDict = self.home;
        cell.team = @"[主队]";
        cell.teamNameLabel = self.model.homename;
        cell.teamIcon = self.model.homelogo;
        cell.isHome = YES;
        
    }else if (indexPath.row == 1){
        
        cell.teamDict = self.away;
        cell.team = @"[客队]";
        cell.teamNameLabel = self.model.awayname;
        cell.teamIcon = self.model.awaylogo;
    }
    
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.row == 0) {
//        
//        self.teamCell.teamDict = self.home;
//    }else if (indexPath.row == 1){
//        self.teamCell.teamDict = self.away;
//    }
//    
//    CGFloat H = [self.teamCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height+1;
//    return H;
//}

#pragma mark - setter&getter

- (void)setModel:(ZGLMatchModel *)model
{
    _model = model;
}

- (NSDictionary *)home
{
    if (!_home) {
        _home = [NSDictionary dictionary];
    }
    return _home;
}
- (NSDictionary *)away
{
    if (!_away) {
        _away = [NSDictionary dictionary];
    }
    return _away;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        [_scrollView setPagingEnabled:YES];
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            NSLog(@"11.0f");
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            NSLog(@"10f");
        }
    }
    return _scrollView;
}

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc]initWithFrame:CGRectZero];
//        if (@available(iOS 11.0, *)) {
//            _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        }
//        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.baidu.com"]];
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [_webView loadRequest:request];
    }
    return _webView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = ViewBackgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            NSLog(@"11.0f");
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            NSLog(@"10f");
        }
    }
    return _tableView;
}

- (NSMutableArray<ZGLIntelligenceModel *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - data

- (void)getMessageData
{
    [sZGLHTTPRequestManager getMessageDataWithMatchID:self.model.matchid Success:^(id responsedata) {
        
        NSDictionary *dict = responsedata[@"matchinfo"];
        _winID = dict[@"win007id"];
        //分析web
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://betcat.zeeging.com/index.php/Api/getAnalysisHtml?winid=%@",_winID]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        //数据拆分
        NSDictionary *intelligenceDict = responsedata[@"intelligence"];
        NSDictionary *homeDict = intelligenceDict[@"home"];
        _home = homeDict;
        NSDictionary *awayDict = intelligenceDict[@"away"];
        _away = awayDict;
        NSArray *array = [NSArray arrayWithObjects:_home,_away, nil];
        
        [_dataArray removeAllObjects];
        _dataArray = [ZGLIntelligenceModel mj_keyValuesArrayWithObjectArray:array];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_tableView reloadData];
            [_webView loadRequest:request];
        });

        DLog(@"self.dataArray--%@",self.dataArray);
        
    } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
        
    }];
    
    
}

#pragma mark - scrollViewDelegate


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = round(scrollView.contentOffset.x / srcW);
    
    [self.headerView changeMenuSelectedWith:index];
    
}


@end


